package com.graduation.cdm.funactivity.view.mineview;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;

public class ModifyPwdActivity extends AppCompatActivity implements View.OnClickListener {

    private Integer userId;
    private EditText new_psw;
    private EditText old_psw;
    private Button psd_val;
    private ImageView iv_back1;
    private TextView tv_ok;
    private RelativeLayout rl_new;
    private EditText confir_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);

        initView();

    }

    private void initView(){
        new_psw = ((EditText) findViewById(R.id.new_psw));
        old_psw = ((EditText) findViewById(R.id.old_psw));
        confir_psw = ((EditText) findViewById(R.id.confir_psw));
        psd_val = ((Button) findViewById(R.id.psd_val));
        iv_back1 = ((ImageView) findViewById(R.id.iv_back1));
        tv_ok = ((TextView) findViewById(R.id.tv_ok));
        rl_new = ((RelativeLayout) findViewById(R.id.rl_new));

        psd_val.setOnClickListener(this);
        iv_back1.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
    }

    private void valPwd(){
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String userPassword = sharedPreferences.getString("userPassword",null);
        userId = sharedPreferences.getInt("userId",0);
        String oldPwd = old_psw.getText().toString();
        if (userPassword.trim().equals(oldPwd.trim())){
            Toast.makeText(getApplicationContext(),"输入正确！",Toast.LENGTH_SHORT).show();
            rl_new.setVisibility(View.VISIBLE);
        }else {
            System.out.println("sssssssssssssssssssssssssss");
            Toast.makeText(getApplicationContext(),"原密码错误！",Toast.LENGTH_SHORT).show();
        }

    }

    private void modifyPassword(){
        String url = preUrl+"user/changeUserInfo";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("=change="+s.trim());
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.get("result").toString().equals("true")) {
                        SharedPreferences sharedPreferences = getSharedPreferences("user_info",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("userPassword",new_psw.getText().toString());
                        editor.commit();

                        Toast.makeText(getApplicationContext(),"修改成功！",Toast.LENGTH_SHORT).show();
                        Intent intent1=new Intent();
                        setResult(200,intent1);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("userId",userId+"");
                map.put("userPassword",new_psw.getText().toString());
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_ok:
                if (new_psw.getText().toString().trim().equals(confir_psw.getText().toString().trim())){
                    modifyPassword();
                }else {
                    Toast.makeText(getApplicationContext(),"请确保两次新密码一致！",Toast.LENGTH_SHORT).show();
                }
                break;
            case  R.id.iv_back1:
                finish();
                break;
            case R.id.psd_val:
                valPwd();
                break;
        }
    }
}
